﻿using blackhouse.BlackApart.Business.Reservations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace blackhouse.BlackApart.Business.Discounts {
    internal class DiscountProvider {

        #region Fields

        private static IDiscount[] variantDiscounts;

        #endregion

        #region Properties

        internal DateTime ArrivalDate { get; private set; }
        internal DateTime DepartureDate { get; private set; }
        internal int Peoples { get; private set; }

        #endregion

        #region Constructors

        public DiscountProvider(DateTime arrivalDateTime, DateTime departureDateTime, int forPeopleCount) {
            this.ArrivalDate = arrivalDateTime;
            this.DepartureDate = departureDateTime;
            this.Peoples = forPeopleCount;
            if (variantDiscounts == null)
                variantDiscounts = this.GetVariantDiscounts();
        }

        #endregion

        #region Methods

        private IDiscount[] GetVariantDiscounts() {
            DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Setup"));
            d = d.GetDirectories("ProviderConfig").FirstOrDefault();
            if (d == null) return new IDiscount[0];
            d = d.GetDirectories("discounts").FirstOrDefault();
            if (d == null) return new IDiscount[0];
            FileInfo[] configFiles = d.GetFiles("*.config");
            List<IDiscount> discounts = new List<IDiscount>(configFiles.Length);
            foreach (FileInfo cf in configFiles) {
                IDiscount vd = this.ReadDiscountFromConfigFile(cf);
                if (vd != null)
                    discounts.Add(vd);
            }
            return discounts.ToArray();
        }

        private IDiscount ReadDiscountFromConfigFile(FileInfo f) {
            XmlDocument xml = new XmlDocument();
            xml.Load(f.FullName);
            XmlNodeList xl = xml.GetElementsByTagName("Type");
            if (xl.Count != 1) return null;
            string typeName = xl[0].InnerText.Trim();
            if (string.IsNullOrEmpty(typeName)) return null;
            Type t = Type.GetType(typeName, false);
            if (t == null) return null;
            IDiscount discount = null;
            try { discount = Activator.CreateInstance(t) as IDiscount; }
            catch { }
            if (discount != null) {
                xl = xml.GetElementsByTagName("Configuration");
                if (xl.Count == 1) {
                    discount.SetXmlConfiguration(xl[0] as XmlElement);
                }
            }
            return discount;
        }

        internal void Apply(Variant variant) {
            foreach (IDiscount discount in variantDiscounts) {
                discount.Apply(variant, this.ArrivalDate, this.DepartureDate, this.Peoples);
            }
        }

        #endregion

    }
}
